package com.fog.judge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fog.judge.common.UserContext;
import com.fog.judge.dto.CourseTeacherDto;
import com.fog.judge.dto.TeamTeacherDto;
import com.fog.judge.entity.*;
import com.fog.judge.mapper.CourseMapper;
import com.fog.judge.mapper.TeamsMapper;
import com.fog.judge.service.CourseService;
import com.fog.judge.service.TeacherService;
import com.fog.judge.service.TeamsService;
import com.fog.judge.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TeamsServiceImpl extends ServiceImpl<TeamsMapper, Teams> implements TeamsService {

    @Resource
    private TeacherService teacherService;
    @Resource
    private TeamsService teamsService;
    @Resource
    private UserService userService;
    @Override
    @Transactional
    public Page<TeamTeacherDto> teamSearch(int page, int pageSize) {
        int userId = UserContext.getUserId();

        Teacher teacher = teacherService.getByTeacherId(userId);

        int teamId = teacher.getTeamId();

        Page<Teacher> pageInfo = new Page<>(page, pageSize);

        Page<TeamTeacherDto> teamTeacherDtoPage = new Page<>();

        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();

        teacherQueryWrapper.orderByDesc("id");

        teacherQueryWrapper.eq("team_id",teamId);

        teacherQueryWrapper.notLike("user_id",userId);
        teacherService.page(pageInfo,teacherQueryWrapper);

        BeanUtils.copyProperties(pageInfo, teamTeacherDtoPage);

        List<Teacher> records = pageInfo.getRecords();

        List<TeamTeacherDto> recordsDto = records.stream().map((item)->{
                TeamTeacherDto teamTeacherDto = new TeamTeacherDto();
                BeanUtils.copyProperties(item, teamTeacherDto);
                teamTeacherDto.setTeacherName(userService.getById(item.getUserId()).getFullName());
                teamTeacherDto.setTeamName(teamsService.getById(teamId).getName());
                return teamTeacherDto;
        }).collect(Collectors.toList());
        teamTeacherDtoPage.setRecords(recordsDto);
        teamTeacherDtoPage.setRecords(recordsDto);
        return teamTeacherDtoPage;
    }

}
